/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package br.gov.cemaden.library.model.smp;

import br.gov.cemaden.library.model.BaseEntity;
import java.io.Serializable;
import java.util.Date;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToOne;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.Temporal;
import org.codehaus.jackson.annotate.JsonBackReference;
import org.codehaus.jackson.annotate.JsonManagedReference;

/**
 *
 * @author paulo.dias
 */
@Entity
@Table(name = "smp_exec_hardware", schema = "smp")
@SequenceGenerator(name = "sequence", sequenceName = "smp.smp_exec_hardware_sequence", schema = "smp")
@NamedQueries({
    @NamedQuery(name = "ExecHardware.findAll", query = "SELECT a FROM ExecHardware a ORDER BY a.id ASC"),
    @NamedQuery(name = "ExecHardware.findById", query = "SELECT a FROM ExecHardware a WHERE a.id = :id"),
    @NamedQuery(name = "ExecHardware.findByData", query = "SELECT a FROM ExecHardware a WHERE a.data < :data"),
    @NamedQuery(name = "ExecHardware.findByWhoSys", query = "SELECT a FROM ExecHardware a WHERE a.whoSys = :whoSys"),})
public class ExecHardware implements Serializable, Comparable<ExecHardware>, BaseEntity {

    @Id
    @GeneratedValue(generator = "sequence", strategy = GenerationType.AUTO)
    @Column(name = "id_exec_hardware", nullable = false, unique = true)
    private Long id;

    @Column(name = "dataAtual")
    @Temporal(javax.persistence.TemporalType.TIMESTAMP)
    private Date data;

    @ManyToOne(cascade = CascadeType.PERSIST)
    @JoinColumn(name = "whoSys")
    @JsonBackReference
    private WhoSys whoSys;

    @OneToOne(mappedBy = "execHardware", cascade = CascadeType.ALL)
    @JsonManagedReference
    private CPU cpu;

    @OneToOne(mappedBy = "execHardware", cascade = CascadeType.ALL)
    @JsonManagedReference
    private LoadAverage load;

    @OneToOne(mappedBy = "execHardware", cascade = CascadeType.ALL)
    @JsonManagedReference
    private Memoria memoria;

    @OneToOne(mappedBy = "execHardware", cascade = CascadeType.ALL)
    @JsonManagedReference
    private Swap swap;

    @OneToOne(mappedBy = "execHardware", cascade = CascadeType.ALL)
    @JsonManagedReference
    private Uptime uptime;

    @OneToOne(mappedBy = "execHardware", cascade = CascadeType.ALL)
    @JsonManagedReference
    private ThreadsCPU threadcpu;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Date getData() {
        return data;
    }

    public void setData(Date data) {
        this.data = data;
    }

    public WhoSys getWhoSys() {
        return whoSys;
    }

    public void setWhoSys(WhoSys whoSys) {
        this.whoSys = whoSys;
    }


    public CPU getCpu() {
        return cpu;
    }

    public void setCpu(CPU cpu) {
        this.cpu = cpu;
    }

    public LoadAverage getLoad() {
        return load;
    }

    public void setLoad(LoadAverage load) {
        this.load = load;
    }

    public Memoria getMemoria() {
        return memoria;
    }

    public void setMemoria(Memoria memoria) {
        this.memoria = memoria;
    }

    public Swap getSwap() {
        return swap;
    }

    public void setSwap(Swap swap) {
        this.swap = swap;
    }

    public Uptime getUptime() {
        return uptime;
    }

    public void setUptime(Uptime uptime) {
        this.uptime = uptime;
    }

    public ThreadsCPU getThreadcpu() {
        return threadcpu;
    }

    public void setThreadcpu(ThreadsCPU threadcpu) {
        this.threadcpu = threadcpu;
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 19 * hash + (this.id != null ? this.id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final ExecHardware other = (ExecHardware) obj;
        if (this.id != other.id && (this.id == null || !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "ExecHardware{" + "id=" + id + ", data=" + data + ", WhoSys=" + whoSys + '}';
    }

    public int compareTo(ExecHardware o) {
        if (this.getData().before(o.getData())) {
            return -1;
        } else if (this.getData().after(o.getData())) {
            return 1;
        }
        if (getId() == null) {
            if (o.getId() == null) {
                return 0;//sao iguais e null
            } else {
                return -1;
            }
        } else if (o.getId() == null) {
            return 1;
        }
        return 0;
    }

}
